﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PookSistener.SystemConfig
{
    public partial class MainCategory : Form
    {

        OleDbConnection conn = null;
        string MODE = "";

        List<Category> category = new List<Category>();

        public MainCategory()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
            panel1.Left = this.Left + 5;
            panel1.Top = this.Top + 5;
            panel1.Width = this.Width - 5;
            panel1.Height = this.Height - 5;

            MODE = "ADD";

            /*
            MainCategoryAdd frm = new MainCategoryAdd();
            frm.MODE = "ADD";
            frm.ShowDialog();

            dataGridView1.DataSource = null;

            ListAll();

            dataGridView1.DataSource = category;
            dataGridView1.Invalidate();
            */
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {
                   
                    if (DialogResult.OK == MessageBox.Show("คุณต้องการลยข้อมูลนี้ใช่หรือไม่", "Info", MessageBoxButtons.OKCancel))
                    {
                        DataGridViewRow row = dataGridView1.SelectedRows[0];

                        try
                        {
                            int id = Convert.ToInt32(row.Cells["ID"].Value);

                            OleDbCommand cmd = new OleDbCommand();
                            cmd.CommandText = "DELETE FROM Category WHERE ID=@ID";
                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = conn;
                            cmd.Parameters.Clear();

                            cmd.Parameters.Add("@ID", OleDbType.Integer).Value = id;

                            cmd.ExecuteNonQuery();

                            MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว");

                            ListAll();

                            dataGridView1.DataSource = null;
                            dataGridView1.DataSource = category;
                            dataGridView1.Invalidate();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error : " + ex.Message);
                        }
                    }
                }
            }
        }

        private void ListAll()
        {
            category.Clear();

            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "SELECT * FROM Category";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = conn;

            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                Category item = new Category();
                item.ID = reader.GetInt32(0);
                item.Name = reader.GetString(1);

                category.Add(item);
            }

            reader.Close();
        }

        private void MainCategory_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;

            conn = new OleDbConnection(PookSistener.Properties.Settings.Default.DatabaseConnectionString);
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }

            conn.Open();

            ListAll();

            dataGridView1.DataSource = null;
            dataGridView1.DataSource = category;
            dataGridView1.Invalidate();

            panel1.Visible = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {

                if (dataGridView1.SelectedRows.Count > 0)
                {
                    DataGridViewRow row = dataGridView1.SelectedRows[0];

                    txtID.Text = row.Cells["ID"].Value.ToString();
                    txtName.Text = row.Cells["tName"].Value.ToString();


                    panel1.Visible = true;

                    panel1.Left = this.Left + 5;
                    panel1.Top = this.Top + 5;
                    panel1.Width = this.Width - 5;
                    panel1.Height = this.Height - 5;

                    MODE = "EDIT";



                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (MODE.Equals("ADD"))
            {
                string sql = "INSERT INTO Category (Name) VALUES (@Name)";

                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                cmd.Connection = conn;

                cmd.Parameters.Clear();
                cmd.Parameters.Add("@Name", OleDbType.VarChar).Value = txtName.Text;

                cmd.ExecuteNonQuery();

                MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว");
                txtName.Text = "";
                txtID.Text = "";
                MODE = "";

                panel1.Visible = false;


                ListAll();

                dataGridView1.DataSource = null;
                dataGridView1.DataSource = category;
                dataGridView1.Invalidate();

                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;

            }
            else if (MODE.Equals("EDIT"))
            {
                string sql = "UPDATE Category SET Name=@Name WHERE ID=@ID";

                try
                {
                    int id = Convert.ToInt32(txtID.Text);

                    OleDbCommand cmd = new OleDbCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = sql;
                    cmd.Connection = conn;

                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@Name", OleDbType.VarChar).Value = txtName.Text;
                    cmd.Parameters.Add("@ID", OleDbType.Integer).Value = id;

                    int affected_row = cmd.ExecuteNonQuery();

                    MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว");

                    panel1.Visible = false;
                    txtName.Text = "";
                    txtID.Text = "";
                    MODE = "";


                    ListAll();

                    dataGridView1.DataSource = null;
                    dataGridView1.DataSource = category;
                    dataGridView1.Invalidate();


                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error : " + ex.Message);
                }
            }
        }

        private void dataGridView1_Resize(object sender, EventArgs e)
        {
            int scrollBarWidth = SystemInformation.VerticalScrollBarWidth + 5;
            int rowHeaderWidth = dataGridView1.RowHeadersWidth + 3;

            int width = dataGridView1.Width - scrollBarWidth;

            int remainingWidth = 0;

            dataGridView1.Columns["tName"].Width = (width - remainingWidth);
            // dataGridView1.Columns["colRemaining"].Width = 250;
        }
    }
}
